//
//  recMasterTableViewController.swift
//  LeDian
//
//  Created by 三秒文化 on 16/3/1.
//  Copyright © 2016年 51ledian. All rights reserved.
//

import UIKit
import Alamofire
import MJRefresh

class LDJobFinderFoundVC: LDBaseTableViewVC {
    
    var pageNum_job = 0
    var removeCacheIdentify = 0
    
    var noticeLab:UILabel?
    //jobs 数组
    var jobsArr = [NSDictionary]() {
        
        didSet{
            
            tableView.reloadData()
            updateFrame()
        }
    }
    
    //外部参数入口
    var models:JobFinderInfoModel?
    
    var searchText = String()
    var searchFiterType = String()
    var searchFiterID:Int?
    var serachPage:Int = 0
    //
    var hasNextPage = 0
    
    private let CellID = "cells"
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        setupTableView()
        setRefresh()
        
    }
    
    func setupTableView(){
        
        view.backgroundColor = LDBackGoundColorGray
        tableView.separatorStyle = .None
        
//        tableView.translatesAutoresizingMaskIntoConstraints = false
//        tableView.snp_makeConstraints { (make) in
//            make.left.bottom.right.equalTo(0)
//            make.top.equalTo(10)
//        }
        tableView.contentInset = UIEdgeInsetsMake(8, 0, 64 + 33 + 8, 0)
        tableView.alwaysBounceHorizontal = false
        tableView.alwaysBounceVertical = true
        
        
        noticeLab = UILabel.setDetailsLab("没有搜索到内容", fontSize: 15, titleColor: LDFontColor.DeepGray, Alignment: .Center)
        noticeLab?.frame = CGRectMake(0, 200, LDFrame.ScreenWidth, 35)
        noticeLab?.hidden = true
        
        view.addSubview(noticeLab!)
    }
    
    func setRefresh() {
        
        let header = LDBaseRefreshView.sharedInstance.header { [weak self] in
            
            self?.refreshNew()
            self?.tableView.mj_header.endRefreshing()
        }
        tableView.mj_header = header

        
        let footer = LDBaseRefreshView.sharedInstance.footer { [weak self] in
            
            self?.refreshMore()
            self?.tableView.mj_footer.endRefreshing()
        }
        
        footer.automaticallyChangeAlpha = true
        tableView.mj_footer = footer
    }
    
    func updateFrame() {
        
        if !(jobsArr.count > 0) {
            
            noticeLab?.hidden = false
        
        } else {
            
            noticeLab?.hidden = true
        }
        
    }
    
    // MARK: - 发送网络请求
 
    func refreshNew() {
        
        LDUserInfoNet.sharedInstance.searchJobs(page: 0, filterid: nil, filtertype: nil, text: searchText, success: { [weak self] (hasNextPage, jobsArr) in
            
            self?.hasNextPage = hasNextPage
            
            if hasNextPage == 0 {
                self?.tableView.mj_footer.hidden = true
            } else {
                self?.tableView.mj_footer.hidden = false
            }
            
            for item in jobsArr {
                
                if self?.jobsArr.contains(item) == false {
                    
                    self?.jobsArr.insert(item, atIndex: 0)
                }
            }
           
            
            self?.jobsArr = jobsArr
            
        }) { (data) in
        }
     
    }
    
    //
    func refreshMore() {

        if hasNextPage == 1 {
            serachPage += 1
            
            LDUserInfoNet.sharedInstance.searchJobs(page: serachPage, filterid: searchFiterID, filtertype: searchFiterType, text: searchText, success: { [weak self] (hasNextPage, jobsArr) in
                
                self?.hasNextPage = hasNextPage
                if hasNextPage == 0 {
                    
                    self?.tableView.mj_footer.hidden = true
                } else {
                    self?.tableView.mj_footer.hidden = false
                }
                
                self?.jobsArr.appendContentsOf(jobsArr)

            }) { (data) in
                
            }
        }
        
    }
}

extension LDJobFinderFoundVC {
    
    // MARK: - Table view data source
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return jobsArr.count
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    

        if DDClientType.Cattle == DDClientInfoManager.clientType() {
            
            
            var cell = tableView.dequeueReusableCellWithIdentifier(CellID) as? RecommendJobTableCell
            
            if cell == nil {
                cell = RecommendJobTableCell(style: .Default, reuseIdentifier: CellID)
            }
            let infoDict = jobsArr[indexPath.row]
            cell?.models = JobsInfomationModel.yy_modelWithDictionary(infoDict as [NSObject : AnyObject])!
            
            return cell!
            
        } else {
            
            var cell = tableView.dequeueReusableCellWithIdentifier(CellID) as? RecommendJobFinderCell
            
            if cell == nil {
                cell = RecommendJobFinderCell(style: .Default, reuseIdentifier: CellID)
            }
            let infoDict = jobsArr[indexPath.row]
            cell?.models = JobFinderInfoModel.yy_modelWithDictionary(infoDict as [NSObject : AnyObject])!
            
            return cell!
        }
        
       
    }
    //
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return LDCellFrame.RectJobFinderCellH
    }
    //
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        if DDClientInfoManager.clientType() == DDClientType.Cattle {
            
            let vc = DDWorkDetailViewController()
            let jobDetail = DDJobsDetail.yy_modelWithDictionary(jobsArr[indexPath.row] as [NSObject : AnyObject])
            vc.title = jobDetail?.jobtitle
            vc.jobDetail = jobDetail
            navigationController?.pushViewController(vc, animated: true)
            
        } else {
         
            let vc = LDJobfinderDetailBaseVCInAll_Show()
            
            let jobFinderDetail = JobFinderInfoModel.yy_modelWithDictionary(jobsArr[indexPath.row] as [NSObject : AnyObject])
            guard let userId = jobFinderDetail?.userid else {
                
                LDShowHUB.shareInstance.showToastNoti("获取用户信息失败")
                return
            }
            
            vc.userID = "\(userId)"
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    //
    override func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        
        self.navigationController?.view.endEditing(true)
    }
    
}


